using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using ClientGUI.Modules.Drawing;
using System.Drawing;

//TODO:  Not yet Text compliant.

namespace ClientGUI
{
    /// <summary>
    /// This is a module useful for drawin list items (not lists themselves)
    /// </summary>
    class List : Module, ITextAble
    {
        XmlNode my_doc;
        XmlNode my_config;

        DisplayArea my_display;
        DisplayArea my_hoover_display;
        DisplayArea my_selected_display;

        public List(XmlNode doc, Module parent,
            DisplayDocument host, XmlNode modconfig,
            TagConfig tagconfig)
            : base(doc, parent, host, tagconfig)
        {
            my_doc = doc;
            my_config = modconfig;

            my_left_buffer = 23;
            my_top_buffer = 3;
            my_top_space = 5;
            my_bottom_space = 0;
            my_right_buffer = 4;
            my_bottom_buffer = 3;

            my_min_height = 24;
        }



        public override void Space(int width)
        {
            int oldWidth = this.Width;
            int oldHeight = this.Height;

            base.Space(width);

            //if we need to cause new sizes
            if (oldHeight != Height || oldWidth != width)
            {
                my_left_buffer = (Parent as ListSquare).BulletWidth();
            }
        }

        public void RenderReg()
        {
            Bitmap my_render = new Bitmap(Width, Height,
                                        System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Point bullet = new Point(0, 0);
            Graphics g_r = Graphics.FromImage(my_render);
            (Parent as ListSquare).DrawBullet(g_r, bullet, Color.FromArgb(160, 160, 160), this);
            my_display = new BitmapDisplayArea(my_render);
        }
        public void RenderWrap()
        {
            Bitmap my_select_render = new Bitmap(Width, Height,
                                        System.Drawing.Imaging.PixelFormat.Format32bppArgb);


            Graphics g_s_r = Graphics.FromImage(my_select_render);

            Point bullet = new Point(0, 0);

            (Parent as ListSquare).DrawBullet(g_s_r, bullet, Color.FromArgb(70, 70, 70), this);

            g_s_r.DrawLine(new Pen(Color.LightGray, 0.1f), new Point(0, 0), new Point(this.Width - 1, 0));
            g_s_r.DrawLine(new Pen(Color.LightGray, 0.1f), new Point(0, 0), new Point(0, this.Height - 1));
            g_s_r.DrawLine(new Pen(Color.DarkGray, 2f), new Point(0, this.Height - 1), new Point(this.Width - 1, this.Height - 1));
            g_s_r.DrawLine(new Pen(Color.DarkGray, 2f), new Point(this.Width - 1, 0), new Point(this.Width - 1, this.Height - 1));

            my_selected_display = new BitmapDisplayArea(my_select_render);
        }
        public void RenderHover()
        {
            Bitmap my_hover_render = new Bitmap(Width, Height,
                                           System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Point bullet = new Point(0, 0);
            Graphics g_h_r = Graphics.FromImage(my_hover_render);
            (Parent as ListSquare).DrawBullet(g_h_r, bullet, Color.FromArgb(120, 120, 120), this);

            g_h_r.DrawRectangle(new Pen(Color.LightGray, 0.1f), new Rectangle(0, 0, Width - 1, Height - 1));
            my_hoover_display = new BitmapDisplayArea(my_hover_render);
        }

        public override Modules.Drawing.DisplayArea GetDisplayArea()
        {
            if (selected)
            {
                if (my_selected_display == null) RenderWrap();
                return my_selected_display;
            }
            else if (hoovered)
            {
                if (my_hoover_display == null) RenderHover();
                return my_hoover_display;
            }
            else
            {
                if (my_display == null) RenderReg();
                return my_display;
            }
        }




        #region ITextAble Members

        public bool Pure
        {
            get
            {
                foreach (Module kid in Children)
                {
                    if ((kid as ITextAble) == null)
                    {
                        return false;
                    }
                    if (!(kid as ITextAble).Pure)
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        public string Text
        {
            get
            {
                StringBuilder text = new StringBuilder();
                foreach (Module kid in Children)
                {
                    if ((kid as ITextAble) != null &&
                        (kid as ITextAble).Pure)
                    {
                        text.Append((kid as ITextAble).Text);
                    }
                    else
                        break;
                }
                return text.ToString() + " ";
            }
        }

        #endregion

        public override void Dispose()
        {
            base.Dispose();
            my_hoover_display.Dispose();
            my_display.Dispose();
            my_selected_display.Dispose();
        }
    }
}
